<?php    
    echo '<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>PHP QR Code Demo</title>
        <script type="text/javascript" src="lib/js/qrcanvas.js"></script>
    </head>
    <body>
    <h1>PHP QR Code</h1>
    <hr/>';

    //引入二维码保存路径
    $FILE_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
    
    //二维码保存目录
    $FILE_WEB_DIR = 'temp/';

    include "lib/full/qrlib.php";    
    
    //如果二维码保存目录不存在则创建目录
    if (!file_exists($FILE_TEMP_DIR))
        mkdir($FILE_TEMP_DIR);
    
    //二维码容错率
    $errorCorrectionLevel = 'L'; 
    if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H')))
        $errorCorrectionLevel = $_REQUEST['level'];    
	
	//二维码尺寸
    $matrixPointSize = 4; 
    if (isset($_REQUEST['size']))
        $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);

	//转换为二维码的字符串
    $textData = 'PHP QR Code :)'; 
    if (isset($_REQUEST['data']) && (trim($_REQUEST['data']) != '')) { 
        $textData = $_REQUEST['data'];
    }
   
    
    echo '<form action="index.php" method="post">
        Data:&#160;<input name="data" value="'.(isset($_REQUEST['data'])?htmlspecialchars($_REQUEST['data']):'PHP QR Code :)').'" />&#160;
        ECC:&#160;<select name="level">
            <option value="L"'.(($errorCorrectionLevel=='L')?' selected="true"':'').'>L - smallest</option>
            <option value="M"'.(($errorCorrectionLevel=='M')?' selected="true"':'').'>M</option>
            <option value="Q"'.(($errorCorrectionLevel=='Q')?' selected="true"':'').'>Q</option>
            <option value="H"'.(($errorCorrectionLevel=='H')?' selected="true"':'').'>H - best</option>
        </select>&#160;
        Size:&#160;<select name="size">';
        
    for($i=1;$i<=10;$i++)
        echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected="true"':'').'>'.$i.'</option>';
        
    echo '</select>&#160;
        <input type="submit" value="GENERATE" /></form><hr/>';

		
    //二维码路径
    $pngFilename = $FILE_TEMP_DIR.'test'.md5($textData.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
    QRcode::png($textData, $pngFilename, $errorCorrectionLevel, $matrixPointSize, 2);    
   
	//显示二维码
    echo '<img src="'.$FILE_WEB_DIR.basename($pngFilename).'" /><hr />';

	
    //显示二维码生成的相关信息
    QRtools::timeBenchmark();    
    
	$logo = "./logo.jpg";
	$QR = $FILE_WEB_DIR.basename($pngFilename);
	
	if ($logo !== FALSE) { 
		$QR = imagecreatefromstring(file_get_contents($QR)); 
		$logo = imagecreatefromstring(file_get_contents($logo)); 
		$QR_width = imagesx($QR);//二维码图片宽度 
		$QR_height = imagesy($QR);//二维码图片高度 
		$logo_width = imagesx($logo);//logo图片宽度 
		$logo_height = imagesy($logo);//logo图片高度 
		$logo_qr_width = $QR_width / 5; 
		$scale = $logo_width/$logo_qr_width; 
		$logo_qr_height = $logo_height/$scale; 
		$from_width = ($QR_width - $logo_qr_width) / 2;
		
		imagealphablending($QR,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
		imagesavealpha($QR,true);//这里很重要,意思是不要丢了$thumb图像的透明色;

		//重新组合图片并调整大小 
		imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, 
		$logo_qr_height, $logo_width, $logo_height); 
	} 
	//输出图片 
	imagepng($QR, 'helloweba.png'); 
	echo '<img src="helloweba.png">'; 

	
    echo '<hr />';

    echo '</body></html>';